package com.hk.blog.init;


import com.hk.blog.domain.Permission;
import com.hk.blog.service.PermissionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;


/**
 * @author Kevin
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PermissionInit implements CommandLineRunner {

    private final PermissionService permissionService;

    @Override
    public void run(String... args) {
        log.info("test...");
//        new Thread(() -> {
//            QueryModel<Permission> query = new QueryModel<>();
//            QueryPage<Permission> queryPage = permissionService.queryForPage(query);
//            log.info("totalRow:{}", queryPage.getTotalRow());
//        }).start();
        Optional<Permission> permission = permissionService.findById(1L);
        CompletableFuture<Optional<Permission>> queryPageFuture = CompletableFuture.supplyAsync(() -> {
            Optional<Permission> permissionOptional = permissionService.findById(2L);
            log.info("exist:{}", permissionOptional.isPresent());
            return permissionOptional;
        });
        Optional<Permission> join = queryPageFuture.join();
//        if (queryPageFuture.isDone()) {
//            try {
//                QueryPage<Permission> queryPage = queryPageFuture.get();
//                System.out.println(queryPage.getData());
//            } catch (Exception e) {
//                //
//            }
//        }
//        Set<String> permissionSet = Arrays.stream(PermissionConstant.class.getDeclaredFields())
//                .filter(field -> Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()))
//                .map(field -> {
//                    try {
//                        Object value = field.get(field.getName());
//                        return value == null ? null : value.toString();
//                    } catch (IllegalAccessException e) {
//                        throw new RuntimeException(e);
//                    }
//                })
//                .filter(Objects::nonNull)
//                .collect(Collectors.toSet());
//        Set<String> existsNames = permissionService.findAll().stream().map(Permission::getPermissionName).collect(Collectors.toSet());
//        Set<String> newPermissionSet = permissionSet.stream()
//                .filter(item -> !existsNames.contains(item)).collect(Collectors.toSet());
//        Permission permission;
//        for (String perms : newPermissionSet) {
//            permission = new Permission();
//            permission.setPermissionName(perms);
//            permissionService.insert(permission);
//        }
    }
}
